//
//  NetWorkViewController.swift
//  test-ios
//
//  Created by wyz5204 on 2023/2/28.
//

import UIKit
import Alamofire
import Moya
/**
 网络的使用类，该类主要进行网络的开发和使用
 */
class NetWorkViewController : UIViewController , UICollectionViewDataSource , UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return date.count
    }
    
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("collectionView  didSelectItemAt \(indexPath.row)")
        switch(date[indexPath.row]){
        case Self.alarmFire :
            test_alarmFire()
        case Self.moyo :
            test_moyo()
        case Self.moyoApi :
            test_moyoAPI()
        case Self.moyoApi_Resopnse :
            test_moyoAPI_Response()
        default :
            return ;
        }
    }
    func test_alarmFire(){
        print("test_alarmFire")
        
        AF.request("https://httpbin.org/get").response { response in
            print("onclick --> \(response)")
        }
    }
    
    
    func test_moyo(){
        print("test_moyo")
        let provider = MoyaProvider<JH>(plugins : [MoyaLoggingPlugin()])
        
        provider.rx.request(.joke(page: 1)).subscribe { event in
            switch event {
            case let .success(response):
                print("success" , response.data)
            case let .failure(error):
                print("failure" ,error)
            }
        }
    }
    
    
    
    func test_moyoAPI(){
        print("test_moyoAPI")
        JHAPI.shared.getJoke(1) { result in
            switch result{
            case .success(let data):
                print(data)
            default :
                print(result)
            }
        }
    }
    
    
    func test_moyoAPI_Response(){
        print("test_moyoAPI_Response")
        JHAPI.shared.getJokeByBaseResponse(1) { result in
            switch result{
            case .success(let data):
                print(data)
            default :
                print(result)
            }
        }
    }
    
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        return collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath).then { cell in
            if(cell.isKind(of: NetWorkCollectionCell.self)){
                let netCell = cell as! NetWorkCollectionCell
                netCell.initBinder(collectionView , date[indexPath.row] ,indexPath)
            }
        }
    }
    
    private static let alarmFire = "Alamofire"
    
    private static let moyo = "moyo"
    
    private static let moyoApi = "moyo-API"
    
    private static let moyoApi_Resopnse = "moyo-API-Resopnse"
    
    private lazy var date :[String] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        date.append(Self.alarmFire)
        date.append(Self.moyo)
        date.append(Self.moyoApi)
        date.append(Self.moyoApi_Resopnse)
        
        let layout  = UICollectionViewFlowLayout().then { layout in
            layout.itemSize = CGSize.init(width: SCREEN_WIDTH , height: 30)
            layout.scrollDirection = UICollectionView.ScrollDirection.horizontal
            layout.minimumLineSpacing = 20
            layout.minimumInteritemSpacing = 20
            layout.sectionInset = UIEdgeInsets.init(top: 20 , left: 20 , bottom: 20  , right: 20 )
        }
        
        UICollectionView.init(frame: self.view.bounds, collectionViewLayout: layout).addTo(self.view) { view in
            view.dataSource = self
            view.delegate = self
//            view.backgroundColor = .red
            view.register(NetWorkCollectionCell.self, forCellWithReuseIdentifier: "cell")
            view.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
        }
    }
    
}
